/*
 * @Author: lcx 792325586@qq.com
 * @Date: 2023-09-17 17:33:31
 * @LastEditors: lcx 792325586@qq.com
 * @LastEditTime: 2023-10-16 22:41:15
 * @FilePath: \组合模式\CompositePattern.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import java.util.ArrayList;
import java.util.List;

abstract class AbstractFile {
    protected String name;
    public abstract void add(AbstractFile fold);
    public abstract void delete(AbstractFile fold);
    public abstract List<AbstractFile> getChildShow();
    public void printName() {
        System.out.println(name);
    }

}

class File extends AbstractFile {

    public File(String name) {
        this.name = name;
    }
    @Override
    public  void add(AbstractFile fold) {
        return;
    }
    @Override
    public void delete(AbstractFile fold) {
        return;
    }
    @Override
    public List<AbstractFile> getChildShow() {
        return null;
    }

}

class Folder extends AbstractFile {
    private List<AbstractFile> list = new ArrayList<AbstractFile>();

    public Folder(String name) {
        this.name = name;
    }
    @Override
    public void add(AbstractFile fold) {
        list.add(fold);
    }
    @Override
    public void delete(AbstractFile fold) {
        list.remove(fold);
    }
    @Override
    public List<AbstractFile> getChildShow() { 
        return list;
    }
    
}

class test {
    public static void main(String[] args) {

        AbstractFile root = new Folder("root");

        AbstractFile floder= new Folder("文件夹");
        AbstractFile fileA = new File("文件A");
        AbstractFile fileB = new File("文件B");
        AbstractFile fileC = new File("文件C");
        AbstractFile fileD = new File("文件D");
        root.add(floder);
        root.add(fileA);
        root.add(fileB);
        floder.add(fileC);
        floder.add(fileD);
        print(root);

    }
    static void print(AbstractFile file) {

        file.printName();
        List<AbstractFile> childrenList = file.getChildShow();
        if (childrenList == null)
            return;
        // if (childrenList != null) {
        // for (int i = 0; i < childrenList.size(); i++) {
        //     //childrenList.get(i).printName();
        //     print(childrenList.get(i));
        // }
        // }
        //增强for循环        
        for (AbstractFile child : childrenList)
        {
            print(child);
        }

    }

}